package com.t282.crm.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.t282.crm.pojo.SalChance;
import com.t282.crm.pojo.SysUser;
import com.t282.crm.service.SalChanceService;

import com.t282.crm.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.json.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr.Chen
 * @since 2021-05-19
 */
@Controller
@RequestMapping("/salChance")
public class SalChanceController {

    @Autowired
    private SalChanceService salChanceService;

    @Autowired
    private SysUserService sysUserService;


    @GetMapping("/list")
    @ApiOperation(value = "跳转到页面")
    public  String getList(){
        return "main";
    }

    @RequestMapping ("/page")
    @ApiOperation(value="获取所有分页信息")
    public String getListPage(@RequestParam(required = false,defaultValue = "1") int pageIndex,
                              @RequestParam(required = false, defaultValue = "") String custName,
                              @RequestParam(required = false, defaultValue = "") String chcDesc,
                              HttpServletRequest request){
        Page<SalChance> page = new Page<>(pageIndex,5);
        QueryWrapper<SalChance> queryWrapper = new QueryWrapper<>();

        if(custName!=null){
            queryWrapper.like("chc_cust_name",custName);
        }
        if(chcDesc!=null){
            queryWrapper.like("chc_desc",chcDesc);
        }

        Page<SalChance> page1 = salChanceService.page(page,queryWrapper);

        if (page1!=null){
            request.setAttribute("data",page1);
            request.setAttribute("message","查询成功！");
            return "/chance/list";
        }else{
            request.setAttribute("message","查询失败！");
            return "/chance/list";
        }
    }

    @PostMapping (value = "/save")
    @ApiOperation(value ="添加修改销售机会")
    public String saveAdd(SalChance salChance,HttpServletRequest request){
        System.out.println(salChance);
        boolean save = salChanceService.save(salChance);


        if (save){
            request.setAttribute("data",save);
            request.setAttribute("message","操作成功！");

            return "redirect:/salChance/page";
        }else{
            request.setAttribute("message","操作失败！");
            return "chance/add";
        }


    }

    @PostMapping (value = "/update")
    @ApiOperation(value ="添加修改销售机会")
    public String update(SalChance salChance,HttpServletRequest request){
        boolean save = salChanceService.updateById(salChance);
        if (save){
            request.setAttribute("data",save);
            request.setAttribute("message","操作成功！");

            return "redirect:/salChance/page";
        }else{
            request.setAttribute("message","操作失败！");
            return "chance/edit";
        }


    }

    @ResponseBody
    @GetMapping("/del/{chcId}")
    @ApiOperation("删除销售机会")
    public Map deleteSal(@PathVariable Long chcId,HttpServletRequest request){

        boolean b = salChanceService.removeById(chcId);
        Map map = new HashMap();

        if (b){
            map.put("delResult","true");
        }else{
            map.put("delResult","false");
        }
        return map;
    }

    @GetMapping("/getadd")
    public String getAdd(){
        return "chance/add";
    }

    @GetMapping("/getedit")
    public String getEdit(@RequestParam Long chcId, HttpServletRequest request){
        SalChance byId = salChanceService.getById(chcId);
        List<SysUser> list = sysUserService.list();
        request.setAttribute("users",list);
        request.setAttribute("chance",byId);
        return "chance/edit";
    }

    @GetMapping("/getedit2")
    public String getEdit2(@RequestParam Long chcId, HttpServletRequest request){
        SalChance byId = salChanceService.getById(chcId);
        List<SysUser> list = sysUserService.list();
        request.setAttribute("users",list);
        request.setAttribute("chance",byId);
        return "chance/edit2";
    }
}

